//
//  LifeBarViewController.m
//  PacIsBack
//
//  Created by Simon André on 03/02/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LifeBarViewController.h"
#import "Constants.h"


@implementation LifeBarViewController

@synthesize maxLife,maxMana,lifeBarView,manaBarView;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

-(UIImage*) calculateCurrentHealthBarImage{
	
	UIImage* fullImage = [UIImage imageNamed:@"fullLifeBar.png"];
	CGFloat width;
	if ([self currentLife] <= 0 || [self maxLife] <= 0) {
		width = 0.0;
	}
	else {
		CGFloat current = [self currentLife];
		CGFloat max = [self maxLife];
		width = (current/max)*[fullImage size].width;
	}
	CGSize imageSize = CGSizeMake(width,[fullImage size].height);
	
	UIGraphicsBeginImageContext(imageSize);
	[fullImage drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
	UIImage* shownImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return shownImage;
}

-(UIImage*) calculateCurrentManaBarImage{
	
	UIImage* fullImage = [UIImage imageNamed:@"fullManaBar.png"];
	CGFloat width;
	if ([self currentMana] <= 0 || [self maxMana] <= 0) {
		width = 0.0;
	}
	else {
		CGFloat current = [self currentMana];
		CGFloat max = [self maxMana];
		width = (current/max)*[fullImage size].width;
	}
	CGSize imageSize = CGSizeMake(width, [fullImage size].height);
	
	UIGraphicsBeginImageContext(imageSize);
	[fullImage drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
	UIImage* shownImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return shownImage;
}

-(id) initWithMaxLife:(NSInteger) maxL andCurrentLife:(NSInteger) curL andCurrentMana:(NSInteger) maxM andCurrentMana:(NSInteger) curM{
	if (self = [super init]) {
		[self setMaxLife:maxL];
		[self setCurrentLife:curL];
		[self setMaxMana:maxM];
		[self setCurrentMana:curM];
		UIView* emptyView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, lifeBarsViewWidth, lifeBarsViewHeight)];
		[self setView:emptyView];
		[emptyView release];
		// life bar empty
		UIImage* image = [UIImage imageNamed:@"emptyBar.png"];
		UIImageView* emptyLifeBarView = [[UIImageView alloc] init];
		[emptyLifeBarView setImage:image];
		[[self view] addSubview:emptyLifeBarView];
		[emptyLifeBarView setCenter:CGPointMake(lifeBarPositionX, lifeBarPositionY)];
		[emptyLifeBarView sizeToFit];
		[emptyLifeBarView release];
		// life bar
		[self updateHealthBar];
		// mana bar empty
		image = [UIImage imageNamed:@"emptyBar.png"];
		UIImageView* emptyManaBarView = [[UIImageView alloc] init];
		[emptyManaBarView setImage:image];
		[[self view] addSubview:emptyManaBarView];
		[emptyManaBarView setCenter:CGPointMake(manaBarPositionX, manaBarPositionY)];
		[emptyManaBarView sizeToFit];
		[emptyManaBarView release];
		// mana bar
		[self updateManaBar];
	}
	return self;
}

-(NSInteger) currentLife{
	return currentLife;
}

-(void) setCurrentLife:(NSInteger) life{
	currentLife = life;
	[self updateHealthBar];
}

-(NSInteger) currentMana{
	return currentMana;
}

-(void) setCurrentMana:(NSInteger) mana{
	currentMana = mana;
	[self updateManaBar];
}

-(void) updateManaBar{
	UIImage* image = [self calculateCurrentManaBarImage];
	UIImageView* fullManaBar = [[UIImageView alloc] init];
	[fullManaBar setImage:image];
	if ([self manaBarView] != nil) {
		[[self manaBarView] removeFromSuperview];
	}
	[self setManaBarView:fullManaBar];
	[fullManaBar release];
	[[self manaBarView] setCenter:CGPointMake(manaBarPositionX, manaBarPositionY)];
	[[self manaBarView] sizeToFit];
	[[self view] addSubview:[self manaBarView]];
	
}

-(void) updateHealthBar{
	UIImage* image = [self calculateCurrentHealthBarImage];
	UIImageView* fullLifeBar = [[UIImageView alloc] init];
	[fullLifeBar setImage:image];
	if ([self lifeBarView] != nil) {
		[[self lifeBarView] removeFromSuperview];
	}
	[self setLifeBarView:fullLifeBar];
	[fullLifeBar release];
	[[self lifeBarView] setCenter:CGPointMake(lifeBarPositionX, lifeBarPositionY)];
	[[self lifeBarView] sizeToFit];
	[[self view] addSubview:[self lifeBarView]];
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[self setLifeBarView:nil];
	[self setManaBarView:nil];
    [super dealloc];
}


@end
